﻿using System;
using System.Globalization;
using System.Text;

namespace PublicDomain.Data.Csv
{
    public static class EnumOperations
    {
        public static bool IsIncluded(this Enum enumValue, Enum enumItem)
        {
            if (enumItem.ToInt32() == enumValue.ToInt32())
            {
                return true;
            }

            if ((enumValue.ToInt32() & enumItem.ToInt32()) == enumItem.ToInt32())
            {
                return true;
            }

            return false;
        }

        public static int ToInt32(this Enum enumValue)
        {
            var result = Convert.ToInt32(enumValue, CultureInfo.InvariantCulture);

            return result;
        }

        public static string EnumNamesToDelimitedText(this Type enumType, char delimiter = ',')
        {
            var enumNames = Enum.GetNames(enumType);

            var result = StringsToDelimitedText(enumNames, delimiter);

            return result;
        }

        private static string StringsToDelimitedText(string[] strings, char delimiter = ',')
        {
            var resultBuilder = new StringBuilder();

            foreach (var item in strings)
            {
                if (resultBuilder.Length > 0)
                {
                    resultBuilder.Append(delimiter);
                    resultBuilder.Append(' ');
                }

                resultBuilder.Append(item);
            }

            var result = resultBuilder.ToString();

            return result;
        }
    }
}
